#[derive(Debug)]
pub struct ForwardList2<T>
where
    T: Sized,
{
    head: Link<T>,
}

#[derive(Debug)]
enum Link<T>
where
    T: Sized,
{
    EMPTY,
    MORE(Box<Node<T>>),
}

#[derive(Debug)]
struct Node<T>
where
    T: Sized,
{
    value: T,      //值
    next: Link<T>, //节点下一个元素用enum全涵盖，要么空，要么是下一个不为空节点
}

impl<T> ForwardList2<T>
where
    T: Sized,
{
    // 初始空头节点
    pub fn new() -> ForwardList2<T> {
        ForwardList2 { head: Link::EMPTY }
    }

    // 插入头节点(头插法)
    pub fn push_head(&mut self, value: T) -> &mut Self {
        self.head = Link::MORE(Box::new(Node {
            value,
            next: std::mem::replace(&mut self.head, Link::EMPTY),
        }));

        self
    }

    // 弹出头节点
    pub fn pop_head(&mut self) -> Option<T> {
        match std::mem::replace(&mut self.head, Link::EMPTY) {
            Link::EMPTY => None,
            Link::MORE(node) => {
                self.head = node.next;
                Some(node.value)
            }
        }
    }
}
